<?php

namespace App\Models\Home;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Part;

class Frame extends Model
{
    protected $table = 'frame';
    protected $guarded = [];

    public function user(){

        return $this->belongsTo('App\User','uid','id');

    }

    public function branch(){

        return $this->belongsTo('App\Models\Home\Branch','branch_id','id');

    }

    public function belong_user(){

        return $this->belongsTo('App\Models\Home\Users','belong','id');

    }

    /**
     * getTree无限递归函数
     * @param $data
     * @param $field_id
     * @param $field_pid
     * @param int $pid
     * @return array
     * @fillable 变量，保存子集数据
     */
    public function getTree($data, $field_id, $field_pid, $pid = 0) {
        $arr = array();
        foreach ($data as $k=>$v) {
            if ($v->$field_pid == $pid) {
                $arr[$k] = $v;
                $arr[$k]['member'] = self::getTree($data, $field_id, $field_pid, $v->$field_id );
                //$arr[$k]['part'] = Part::getPartUserInfo([$v->uid]);
            }
        }
        return $arr;
    }

    /**
     * getArray无限递归函数
     * @param $data
     * @param $field_id
     * @param $field_pid
     * @param int $pid
     * @return array
     * @fillable 变量，保存子集数据
     */
    public function getArray($data, $field_id, $field_pid, $pid = 0) {
        $arr = array();
        foreach ($data as $k=>$v) {
            if ($v->$field_pid == $pid) {
                $arr[] = $v;
                $arr = array_merge($arr,self::getArray($data, $field_id, $field_pid, $v->$field_id ));
            }
        }
        return $arr;
    }

    /**
     * getLeader
     */
    public function getLeader($branch_id=null,$level=3,$flag=true) {
        $level = 3;// 可以随意转换身份
        $data = self::where('status',1)->with([
            'user'=>function($query){
                $query->where('status',1);
            },
            'branch'=>function($query){
                $query->where('status',1);
            }
        ])->get();

        $arr = array();
        foreach ($data as $k => $v) {
            if ($v->user){
                $v->user_name = $v->user->username;
                if ($v->level < $level && $v->branch_id == $branch_id) {
                    $arr[] = $v;
                }else if($v->level < $level && $flag){
                    $branch_name = $v->branch?$v->branch->name:$v->branch_id;
                    $arr[$branch_name][] = $v;
                }
            }
        }

        return $arr;
    }

    public function getFrameTree($data = array(), $pid = 0) {
        if (empty($data)){
            $data = self::where('status',1)->with('user')->get();
        }
        $field_id = 'id';
        $field_pid = 'belong';
        $arr = $this->getTree($data, $field_id, $field_pid, $pid );
        return $arr;
    }

    public function GetFrame($branch,$level){

        $branch_obj = self::where('status',1)->where('branch_id',$branch)->with(['user'])->get();

        $admin_obj = self::where('status',1)->where('branch_id',$branch)->where('level',$level)->get();

        $list = [];

        foreach ($admin_obj as $k => $v){

            $list[] = $this->getFrameTree($branch_obj,$v->id);

        }

        return $list;
    }

    public function getMember($frame_id = null){
        $data = self::where('status',1)->with('user')->get();
        if ($frame_id){
            $user_frame = self::find($frame_id);
            $pid = $user_frame->id;
        }else{
            $pid = 0;
        }

        $field_id = 'id';
        $field_pid = 'belong';
        $arr = $this->getArray($data, $field_id, $field_pid, $pid );
        $member = [];
        foreach ($arr as $v){
            $member[] = $v->uid;
        }
        return $member;
    }

    /**
     * getTeam
     */
    public function getTeam($id) {
        $data = self::find($id);
        if ($data->level == 2) {
            $team = $data;
        }else{
            $team = new Frame();
        }
        return $team;
    }

    public function GetDelFrame($frame_id = null){
        $data = self::where('status',1)->with('user')->get()->load('user');
        if ($frame_id){
            $user_frame = self::find($frame_id);
            $pid = $user_frame->id;
        }else{
            $pid = 0;
        }

        $field_id = 'id';
        $field_pid = 'belong';
        $arr = $this->getArray($data, $field_id, $field_pid, $pid );

        return $arr;
    }

    public function GetAll(){

         return self::where('status',1)->with(['user','branch'])->get();

    }

    public function GetBranchAll($branch_id=1){

         return self::orderBy('uid','asc')->where('status',1)->where('branch_id',$branch_id)->with(['user'=>function($query){
             $query->withCount('download_log');
         },'branch'])->get();

    }

}
